package com.example.papercut.controller;

import com.example.papercut.entity.User;
import com.example.papercut.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

// 统一认证接口路径
@RestController
@RequestMapping("/api/user") 


public class UserController {

    private final UserService userService;
    private static Integer currentLoggedInUserId = null; 

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    /**
     * POST /api/user/register
     * 用户注册接口
     */
    @PostMapping("/register")
    public ResponseEntity<?> register(@RequestBody Map<String, String> request) {
        try {
            String username = request.get("username");
            String email = request.get("email");
            String password = request.get("password");

            // 简单校验
            if (username == null || password == null || email == null) {
                return ResponseEntity.badRequest().body("用户名、邮箱和密码不能为空。");
            }

            User registeredUser = userService.register(username, email, password);
            
            return ResponseEntity.status(HttpStatus.CREATED).body(
                Map.of("message", "注册成功", "user", registeredUser)
            );
            
        } catch (IllegalArgumentException e) {
            // 用户名或邮箱已存在等业务异常
            return ResponseEntity.badRequest().body(Map.of("message", e.getMessage()));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Map.of("message", "注册失败，服务器内部错误。"));
        }
    }

    /**
     * POST /api/user/login
     * 用户登录接口
     */
    @PostMapping("/login")
    public ResponseEntity<?> login(@RequestBody Map<String, String> request) {

        String identifier = request.get("username"); // 前端可能传用户名或邮箱
        String password = request.get("password");

        if (identifier == null || password == null) {
            return ResponseEntity.badRequest().body(Map.of("message", "用户名/邮箱和密码不能为空。"));
        }

        User user = userService.login(identifier, password);


        if (user != null) {
            // 登录成功，实际项目中这里会生成 JWT Token 并返回
            // 为简化，这里返回用户信息
            currentLoggedInUserId = user.getId(); 
            return ResponseEntity.ok(Map.of("message", "登录成功", "user", user));
        } else {
            // 认证失败
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(Map.of("message", "用户名/邮箱或密码错误。"));
        }
    }

    /**
 * GET /api/user/current
 * 获取当前登录用户的 ID (利用模拟的全局状态)
 */
@GetMapping("/current")
public ResponseEntity<?> getCurrentUser() {
    if (currentLoggedInUserId != null) {
        // 返回用户的 ID
        return ResponseEntity.ok(Map.of("id", currentLoggedInUserId)); 
    } else {
        // 未登录
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(Map.of("message", "用户未登录。"));
    }
}
}